﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using UI.Core.Events;
using UI.Core.Interface;

namespace UI.Module.Admin
{
    [Module(ModuleName = "Admin")]
    public class AdminModule : IModule
    {
        private readonly IUnityContainer _container;

        public AdminModule(IUnityContainer container)
        {
            _container = container;
        }
        public void Initialize()
        {
            var regionManager = _container.Resolve<IRegionManager>();
            regionManager.Regions["MainRegion"].Add(new UsersView(), "UsersView");
            regionManager.Regions["MainRegion"].Add(new ProfilsView(), "ProfilsView");

            // Subscribe to ViewRequestedEvent
            var eventAggregator = _container.Resolve<IEventAggregator>();
            var viewRequestedEvent = eventAggregator.GetEvent<ViewRequestedEvent>();
            viewRequestedEvent.Subscribe(this.ViewRequestedEventHandler, ThreadOption.PublisherThread, true);
        }

        public void ViewRequestedEventHandler(string moduleName)
        {
            var moduleServices = _container.Resolve<IModuleServices>();

            if (moduleName == "UsersView")
            {
                moduleServices.ActivateView("UsersView");
            }
            if (moduleName == "ProfilsView")
            {
                moduleServices.ActivateView("ProfilsView");
            }
        }
    }
}
